﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Workflows.Components.TextMining.Collapse
{
    public class CountryComparer:IAttributeComparer
    {
        private Dictionary<string, string> _CountryNameMappings;

        public CountryComparer()
        {
            this._CountryNameMappings = this.GetCountries();
        }

        public AttributeComparerType ComparerType
        {
            get
            {
                return AttributeComparerType.ClassComparer;
            }
        }

        public SqlDbType FieldDbType
        {
            get
            {
                return SqlDbType.VarChar;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public double Compare(object value1, object value2, double penalty, SqlConnection conn, string unevenDistTableName)
        {
            if (value1 == null || value1 == DBNull.Value)
                return 0;
            if (value2 == null || value2 == DBNull.Value)
                return 0;
            string country1 = value1.ToString().ToUpper();
            string country2 = value2.ToString().ToUpper();
            if (country1.Trim().Length == 0 || country2.Trim().Length == 0)
                return 0;

            if (this._CountryNameMappings != null && this._CountryNameMappings.ContainsKey(country1))
                country1 = this._CountryNameMappings[country1];
            if (this._CountryNameMappings != null && this._CountryNameMappings.ContainsKey(country2))
                country2 = this._CountryNameMappings[country2];
            if (country1 == country2)
                return 1;
            else
                return penalty;
        }

        public object Merge(List<object> values, SqlConnection conn, string unevenDistTableName)
        {
            object mode = null;
            int maxFreq = 0;
            Dictionary<object, int> freqs = new Dictionary<object, int>();
            foreach (object val in values)
            {
                if (val == null || val.ToString().Length == 0)
                    continue;

                if (freqs.ContainsKey(val))
                    freqs[val] = freqs[val] + 1;
                else
                    freqs.Add(val, 1);
            }
            foreach (object val in freqs.Keys)
            {
                if (freqs[val] > maxFreq)
                {
                    maxFreq = freqs[val];
                    mode = val;
                }
            }
            return mode;
        }

        private Dictionary<string, string> GetCountries()
        {
            Dictionary<string, string> countries = new Dictionary<string, string>();
            countries = new Dictionary<string, string>();
            countries.Add("Andorra", "AD");
            countries.Add("United Arab Emirates", "AE");
            countries.Add("Afghanistan", "AF");
            countries.Add("Antigua & Barbuda", "AG");
            countries.Add("Anguilla", "AI");
            countries.Add("Albania", "AL");
            countries.Add("Armenia", "AM");
            countries.Add("Netherlands Antilles", "AN");
            countries.Add("Angola", "AO");
            countries.Add("Antarctica", "AQ");
            countries.Add("Argentina", "AR");
            countries.Add("American Samoa", "AS");
            countries.Add("Austria", "AT");
            countries.Add("Australia", "AU");
            countries.Add("Aruba", "AW");
            countries.Add("Azerbaijan", "AZ");
            countries.Add("Bosnia and Herzegovina", "BA");
            countries.Add("Barbados", "BB");
            countries.Add("Bangladesh", "BD");
            countries.Add("Belgium", "BE");
            countries.Add("Burkina Faso", "BF");
            countries.Add("Bulgaria", "BG");
            countries.Add("Bahrain", "BH");
            countries.Add("Burundi", "BI");
            countries.Add("Benin", "BJ");
            countries.Add("Bermuda", "BM");
            countries.Add("Brunei Darussalam", "BN");
            countries.Add("Bolivia", "BO");
            countries.Add("Brazil", "BR");
            countries.Add("Bahama", "BS");
            countries.Add("Bhutan", "BT");
            countries.Add("Burma (no longer exists)", "BU");
            countries.Add("Bouvet Island", "BV");
            countries.Add("Botswana", "BW");
            countries.Add("Belarus", "BY");
            countries.Add("Belize", "BZ");
            countries.Add("Canada", "CA");
            countries.Add("Cocos (Keeling) Islands", "CC");
            countries.Add("Central African Republic", "CF");
            countries.Add("Congo", "CG");
            countries.Add("Switzerland", "CH");
            countries.Add("Côte D'ivoire (Ivory Coast)", "CI");
            countries.Add("Cook Iislands", "CK");
            countries.Add("Chile", "CL");
            countries.Add("Cameroon", "CM");
            countries.Add("China", "CN");
            countries.Add("Colombia", "CO");
            countries.Add("Columbia", "CO");
            countries.Add("Costa Rica", "CR");
            countries.Add("Czechoslovakia (no longer exists)", "CS");
            countries.Add("Cuba", "CU");
            countries.Add("Cape Verde", "CV");
            countries.Add("Christmas Island", "CX");
            countries.Add("Cyprus", "CY");
            countries.Add("Czech Republic", "CZ");
            countries.Add("German Democratic Republic (no longer exists)", "DD");
            countries.Add("Germany", "DE");
            countries.Add("Djibouti", "DJ");
            countries.Add("Denmark", "DK");
            countries.Add("Dominica", "DM");
            countries.Add("Dominican Republic", "DO");
            countries.Add("Algeria", "DZ");
            countries.Add("Ecuador", "EC");
            countries.Add("Estonia", "EE");
            countries.Add("Egypt", "EG");
            countries.Add("Western Sahara", "EH");
            countries.Add("Eritrea", "ER");
            countries.Add("Spain", "ES");
            countries.Add("Ethiopia", "ET");
            countries.Add("Finland", "FI");
            countries.Add("Fiji", "FJ");
            countries.Add("Falkland Islands (Malvinas)", "FK");
            countries.Add("Micronesia", "FM");
            countries.Add("Faroe Islands", "FO");
            countries.Add("France", "FR");
            countries.Add("France, Metropolitan", "FX");
            countries.Add("Gabon", "GA");
            countries.Add("United Kingdom (Great Britain)", "GB");
            countries.Add("Great Britain", "GB");
            countries.Add("United Kingdom", "GB");
            countries.Add("GB", "GB");
            countries.Add("UK", "GB");
            countries.Add("U.K.", "GB");
            countries.Add("England", "GB");
            countries.Add("Grenada", "GD");
            countries.Add("Georgia", "GE");
            countries.Add("French Guiana", "GF");
            countries.Add("Ghana", "GH");
            countries.Add("Gibraltar", "GI");
            countries.Add("Greenland", "GL");
            countries.Add("Gambia", "GM");
            countries.Add("Guinea", "GN");
            countries.Add("Guadeloupe", "GP");
            countries.Add("Equatorial Guinea", "GQ");
            countries.Add("Greece", "GR");
            countries.Add("South Georgia and the South Sandwich Islands", "GS");
            countries.Add("Guatemala", "GT");
            countries.Add("Guam", "GU");
            countries.Add("Guinea-Bissau", "GW");
            countries.Add("Guyana", "GY");
            countries.Add("Hong Kong", "HK");
            countries.Add("Heard & McDonald Islands", "HM");
            countries.Add("Honduras", "HN");
            countries.Add("Croatia", "HR");
            countries.Add("Haiti", "HT");
            countries.Add("Hungary", "HU");
            countries.Add("Indonesia", "ID");
            countries.Add("Ireland", "IE");
            countries.Add("Israel", "IL");
            countries.Add("India", "IN");
            countries.Add("British Indian Ocean Territory", "IO");
            countries.Add("Iraq", "IQ");
            countries.Add("Islamic Republic of Iran", "IR");
            countries.Add("Iceland", "IS");
            countries.Add("Italy", "IT");
            countries.Add("Jamaica", "JM");
            countries.Add("Jordan", "JO");
            countries.Add("Japan", "JP");
            countries.Add("Kenya", "KE");
            countries.Add("Kyrgyzstan", "KG");
            countries.Add("Cambodia", "KH");
            countries.Add("Kiribati", "KI");
            countries.Add("Comoros", "KM");
            countries.Add("St. Kitts and Nevis", "KN");
            countries.Add("Korea, Democratic People's Republic of", "KP");
            countries.Add("Korea, Republic of", "KR");
            countries.Add("Korea", "KR");
            countries.Add("South Korea", "KR");
            countries.Add("North Korea", "KP");
            countries.Add("Kuwait", "KW");
            countries.Add("Cayman Islands", "KY");
            countries.Add("Kazakhstan", "KZ");
            countries.Add("Lao People's Democratic Republic", "LA");
            countries.Add("Lebanon", "LB");
            countries.Add("Saint Lucia", "LC");
            countries.Add("Liechtenstein", "LI");
            countries.Add("Sri Lanka", "LK");
            countries.Add("Liberia", "LR");
            countries.Add("Lesotho", "LS");
            countries.Add("Lithuania", "LT");
            countries.Add("Luxembourg", "LU");
            countries.Add("Latvia", "LV");
            countries.Add("Libyan Arab Jamahiriya", "LY");
            countries.Add("Morocco", "MA");
            countries.Add("Monaco", "MC");
            countries.Add("Moldova, Republic of", "MD");
            countries.Add("Madagascar", "MG");
            countries.Add("Marshall Islands", "MH");
            countries.Add("Mali", "ML");
            countries.Add("Mongolia", "MN");
            countries.Add("Myanmar", "MM");
            countries.Add("Macau", "MO");
            countries.Add("Northern Mariana Islands", "MP");
            countries.Add("Martinique", "MQ");
            countries.Add("Mauritania", "MR");
            countries.Add("Monserrat", "MS");
            countries.Add("Malta", "MT");
            countries.Add("Mauritius", "MU");
            countries.Add("Maldives", "MV");
            countries.Add("Malawi", "MW");
            countries.Add("Mexico", "MX");
            countries.Add("Malaysia", "MY");
            countries.Add("Mozambique", "MZ");
            countries.Add("Namibia", "NA");
            countries.Add("New Caledonia", "NC");
            countries.Add("Niger", "NE");
            countries.Add("Norfolk Island", "NF");
            countries.Add("Nigeria", "NG");
            countries.Add("Nicaragua", "NI");
            countries.Add("Netherlands", "NL");
            countries.Add("Norway", "NO");
            countries.Add("Nepal", "NP");
            countries.Add("Nauru", "NR");
            countries.Add("Neutral Zone (no longer exists)", "NT");
            countries.Add("Niue", "NU");
            countries.Add("New Zealand", "NZ");
            countries.Add("Oman", "OM");
            countries.Add("Panama", "PA");
            countries.Add("Peru", "PE");
            countries.Add("French Polynesia", "PF");
            countries.Add("Papua New Guinea", "PG");
            countries.Add("Philippines", "PH");
            countries.Add("PHILIPPINE ISLANDS", "PH");
            countries.Add("Pakistan", "PK");
            countries.Add("Poland", "PL");
            countries.Add("St. Pierre & Miquelon", "PM");
            countries.Add("Pitcairn", "PN");
            countries.Add("Puerto Rico", "PR");
            countries.Add("Portugal", "PT");
            countries.Add("Palau", "PW");
            countries.Add("Paraguay", "PY");
            countries.Add("Qatar", "QA");
            countries.Add("Réunion", "RE");
            countries.Add("Romania", "RO");
            countries.Add("Russian Federation", "RU");
            countries.Add("Rwanda", "RW");
            countries.Add("Saudi Arabia", "SA");
            countries.Add("Solomon Islands", "SB");
            countries.Add("Seychelles", "SC");
            countries.Add("Sudan", "SD");
            countries.Add("Sweden", "SE");
            countries.Add("Sweeden", "SE");
            countries.Add("Singapore", "SG");
            countries.Add("St. Helena", "SH");
            countries.Add("Slovenia", "SI");
            countries.Add("Svalbard & Jan Mayen Islands", "SJ");
            countries.Add("Slovakia", "SK");
            countries.Add("Sierra Leone", "SL");
            countries.Add("San Marino", "SM");
            countries.Add("Senegal", "SN");
            countries.Add("Somalia", "SO");
            countries.Add("Suriname", "SR");
            countries.Add("Sao Tome & Principe", "ST");
            countries.Add("Union of Soviet Socialist Republics (no longer exists)", "SU");
            countries.Add("El Salvador", "SV");
            countries.Add("Syrian Arab Republic", "SY");
            countries.Add("Syria", "SY");
            countries.Add("Swaziland", "SZ");
            countries.Add("Turks & Caicos Islands", "TC");
            countries.Add("Chad", "TD");
            countries.Add("French Southern Territories", "TF");
            countries.Add("Togo", "TG");
            countries.Add("Thailand", "TH");
            countries.Add("Tajikistan", "TJ");
            countries.Add("Tokelau", "TK");
            countries.Add("Turkmenistan", "TM");
            countries.Add("Tunisia", "TN");
            countries.Add("Tonga", "TO");
            countries.Add("East Timor", "TP");
            countries.Add("Turkey", "TR");
            countries.Add("Trinidad & Tobago", "TT");
            countries.Add("Tuvalu", "TV");
            countries.Add("Taiwan, Province of China", "TW");
            countries.Add("Republic of China", "TW");
            countries.Add("ROC", "TW");
            countries.Add("Tanzania, United Republic of", "TZ");
            countries.Add("Ukraine", "UA");
            countries.Add("Uganda", "UG");
            countries.Add("United States Minor Outlying Islands", "UM");
            countries.Add("United States of America", "US");
            countries.Add("USA", "US");
            countries.Add("U.S.A.", "US");
            countries.Add("United States", "US");
            countries.Add("Uruguay", "UY");
            countries.Add("Uzbekistan", "UZ");
            countries.Add("Vatican City State (Holy See)", "VA");
            countries.Add("St. Vincent & the Grenadines", "VC");
            countries.Add("Venezuela", "VE");
            countries.Add("British Virgin Islands", "VG");
            countries.Add("United States Virgin Islands", "VI");
            countries.Add("Viet Nam", "VN");
            countries.Add("Vanuatu", "VU");
            countries.Add("Wallis & Futuna Islands", "WF");
            countries.Add("Samoa", "WS");
            countries.Add("Democratic Yemen (no longer exists)", "YD");
            countries.Add("Yemen", "YE");
            countries.Add("Mayotte", "YT");
            countries.Add("Yugoslavia", "YU");
            countries.Add("South Africa", "ZA");
            countries.Add("Zambia", "ZM");
            countries.Add("Zaire", "ZR");
            countries.Add("Zimbabwe", "ZW");

            return countries;
        }
    }
}
